package com.dtsheep.elasticsearch.es.client.document;

import com.dtsheep.elasticsearch.es.client.ElasticsearchClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * BatchInsertDocument
 *
 * <p>
 *     批量插入文档
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/24
 */
public class BatchInsertDocument {

    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = ElasticsearchClientUtil.getEsClient();

        BulkResponse responses = client
                .bulk(new BulkRequest()
                        .add(new IndexRequest().index("user").id("2").source(XContentType.JSON,"name","张三"))
                        .add(new IndexRequest().index("user").id("3").source(XContentType.JSON,"name","李四")), RequestOptions.DEFAULT);

        System.out.println(responses.getTook());
        System.out.println(responses.getItems());
    }
}
